<template>
  <van-nav-bar title="新增收货地址" left-arrow @click-left="onBack" />

  <van-address-edit
    :area-list="areaList"
    show-set-default
    :address-info="addressInfo"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
  />
</template>
<script>
import { areaList } from "@vant/area-data";
import { showNotify } from "vant";
export default {
  data() {
    return {
      areaList,
      addressInfo: {},
      LoginAuth: {},
    };
  },
  created() {
    let LoginAuth = this.$cookies.get("LoginAuth")
      ? this.$cookies.get("LoginAuth")
      : {};

    this.addressInfo = {
      name: LoginAuth.nickname,
      tel: LoginAuth.mobile,
    };

    this.LoginAuth = LoginAuth;
  },
  methods: {
    onBack() {
      this.$router.go(-1);
    },
    async onSave(info) {
      let data = {
        busid: this.LoginAuth.id,
        consignee: info.name,
        mobile: info.tel,
        status: info.isDefault === true ? 1 : 0,
        address: info.addressDetail,
        code: info.areaCode,
      };

      let result = await this.$api.AddressAdd(data);

      if (result.code === 1) {
        showNotify({
          type: "success",
          duration: 1500,
          message: result.msg,
          onClose: () => {
            this.$router.push("/business/address/index");
          },
        });
      } else {
        showNotify({
          type: "warning",
          duration: 1500,
          message: result.msg,
        });
      }
    },
  },
};
</script>